using System.Collections;
using System.Collections.Generic;
using BCW;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;

public class PromoactionsManagerUI : MonoBehaviour
{
	[SerializeField]
	private Texture iconCash;

	[SerializeField]
	private Texture iconGold;

	[SerializeField]
	private Texture iconSubscription;

	[SerializeField]
	private Texture iconAlphaSubscription;

	[SerializeField]
	private PromoItem item;

	[SerializeField]
	private UIGrid _gridPromo;

	[SerializeField]
	private UIGrid _gridSlide;

	[SerializeField]
	private List<PromoItem> listPromo = new List<PromoItem>();

	[SerializeField]
	private List<GameObject> slides = new List<GameObject>();

	[SerializeField]
	private SelectableButtonController controllerButton;

	[SerializeField]
	private SelectableButton slideButton;

	[SerializeField]
	private UICenterOnChild centrOnChield;

	[SerializeField]
	private GameObject mainGO;

	[SerializeField]
	private UIScrollView scrollView;

	[SerializeField]
	private UISprite bgSpriteGreen;

	[SerializeField]
	private UISprite bgSpriteGold;

	public static PromoactionsManagerUI instance;

	private Coroutine slideCoroutine;

	private Tweener bgSpriteTweener;

	private bool leftSlide = true;

	private int indexSlide;

	public MiniStarterPackUI miniStarterPackUI;

	private List<MiniStarterPackUI> miniStarterPackButtons = new List<MiniStarterPackUI>();

	private void Awake()
	{
		instance = this;
		centrOnChield.onCenter = SelectFromCenter;
	}

	private void OnEnable()
	{
		InAppPromoController.promosParsedFromJson += CreatePromo;
		InAppSubscriptionsController.OnJSONParsed += CreatePromo;
		SwitchLanguage.onChangeLanguage += UpdatePromo;
		CreatePromo();
	}

	private void OnDisable()
	{
		InAppPromoController.promosParsedFromJson -= CreatePromo;
		InAppSubscriptionsController.OnJSONParsed -= CreatePromo;
		SwitchLanguage.onChangeLanguage -= UpdatePromo;
		if (slideCoroutine != null)
		{
			StopCoroutine(slideCoroutine);
			slideCoroutine = null;
		}
		ClearMiniStarterPackButtons();
	}

	private void Start()
	{
		CreatePromo();
		StarterPacksManager.instance.onAvailableStarterPacksUpdated += CreatePromo;
	}

	private void OnDestroy()
	{
		instance = null;
		StarterPacksManager.instance.onAvailableStarterPacksUpdated -= CreatePromo;
	}

	public void CreatePromo()
	{
		StartCoroutine(IECreatePromo());
	}

	private IEnumerator IECreatePromo()
	{
		foreach (PromoItem promo3 in listPromo)
		{
			Object.Destroy(promo3.gameObject);
		}
		foreach (SelectableButton point in controllerButton.allButtons)
		{
			Object.Destroy(point.gameObject);
		}
		listPromo.Clear();
		controllerButton.allButtons.Clear();
		slides.Clear();
		if (InAppPromoController.instance != null && InAppPromoController.instance.inAppPromos.allPromos != null)
		{
			foreach (InAppPromoController.InAppPromo promo2 in InAppPromoController.instance.inAppPromos.allPromos)
			{
				if (!promo2.Enabled)
				{
					continue;
				}
				PromoItem promoGO2 = Object.Instantiate(item);
				promoGO2.transform.SetParent(_gridPromo.transform);
				promoGO2.transform.localScale = Vector3.one;
				promoGO2.transform.localPosition = Vector3.zero;
				promoGO2.gameObject.SetActive(true);
				promoGO2.isCashPromo = promo2.isCashPromo;
				promoGO2.info = promo2;
				promoGO2.promoName.text = promo2.GetTitle();
				promoGO2.SetProductPower(promo2.promoBonuses.GetPromoMarketBonusesIds());
				if (promo2.bonusImageType == InAppPromoController.BonusImageType.Money)
				{
					if (promo2.isCashPromo)
					{
						promoGO2.mainPromoTexture.mainTexture = iconCash;
						promoGO2.bonuseMoneyTexture.mainTexture = iconGold;
					}
					else
					{
						promoGO2.mainPromoTexture.mainTexture = iconGold;
						promoGO2.bonuseMoneyTexture.mainTexture = iconCash;
					}
					promoGO2.mainPromoTexture.gameObject.SetActive(true);
					promoGO2.bonuseMoneyTexture.gameObject.SetActive(true);
				}
				else
				{
					promoGO2.mainPromoTexture.gameObject.SetActive(false);
				}
				if (!promo2.promoBonuses.GiveAlternativeBonuses())
				{
					int i = 0;
					UIBasicSprite[] carPromoTexture = promoGO2.carPromoTexture;
					foreach (UIBasicSprite texture2 in carPromoTexture)
					{
						texture2.mainTexture = null;
						texture2.gameObject.SetActive(false);
					}
					UIBasicSprite[] gunPromoTexture = promoGO2.gunPromoTexture;
					foreach (UIBasicSprite texture in gunPromoTexture)
					{
						texture.mainTexture = null;
						texture.gameObject.SetActive(false);
					}
					foreach (string marketItem in promo2.promoBonuses.GetPromoMarketBonusesIds())
					{
						if (promo2.bonusImageType == InAppPromoController.BonusImageType.Vehicle)
						{
							promoGO2.carPromoTexture[i] = promoGO2.carPromoTexture[i].ReplaceForCurrentPlatform();
							promoGO2.carPromoTexture[i].LoadTextureAndAlphaMask("AtlasShopGuns/", MarketController.Instance.GetProduct(marketItem).texture);
							promoGO2.carPromoTexture[i].gameObject.SetActive(true);
							if (MarketController.Instance.GetProduct(marketItem) is ProductTitan)
							{
								promoGO2.carPromoTexture[i].width = 128;
								promoGO2.carPromoTexture[i].height = 128;
							}
						}
						else if (promo2.bonusImageType == InAppPromoController.BonusImageType.Weapon)
						{
							promoGO2.gunPromoTexture[i] = promoGO2.gunPromoTexture[i].ReplaceForCurrentPlatform();
							promoGO2.gunPromoTexture[i].LoadTextureAndAlphaMask("AtlasShopGuns/", MarketController.Instance.GetProduct(marketItem).texture);
							promoGO2.gunPromoTexture[i].gameObject.SetActive(true);
						}
						i++;
					}
					if (promo2.promoBonuses.GetPromoMarketBonusesIds().Count == 1)
					{
						if (promo2.bonusImageType == InAppPromoController.BonusImageType.Vehicle)
						{
							promoGO2.carPromoTexture[0].transform.localScale = Vector3.one * 1.2f;
						}
						else if (promo2.bonusImageType == InAppPromoController.BonusImageType.Weapon)
						{
							promoGO2.gunPromoTexture[0].transform.localScale = Vector3.one * 1.2f;
						}
					}
				}
				else
				{
					promoGO2.mainPromoTexture.mainTexture = iconGold;
					promoGO2.bonuseMoneyTexture.mainTexture = iconCash;
					promoGO2.mainPromoTexture.gameObject.SetActive(true);
					promoGO2.bonuseMoneyTexture.gameObject.SetActive(true);
					promoGO2.SetProductPower(null);
				}
				listPromo.Add(promoGO2);
				slides.Add(promoGO2.gameObject);
				SelectableButton slide2 = Object.Instantiate(slideButton);
				slide2.transform.SetParent(_gridSlide.transform);
				slide2.transform.localScale = Vector3.one;
				slide2.gameObject.SetActive(true);
				controllerButton.allButtons.Add(slide2);
			}
		}
		if (InAppSubscriptionsController.instance != null && InAppSubscriptionsController.instance.Promo.Enabled)
		{
			InAppSubscriptionsController.InAppPromo promo = InAppSubscriptionsController.instance.Promo;
			PromoItem promoGO = Object.Instantiate(item);
			promoGO.transform.SetParent(_gridPromo.transform);
			promoGO.transform.localScale = Vector3.one;
			promoGO.transform.localPosition = Vector3.one * 1000f;
			promoGO.gameObject.SetActive(true);
			promoGO.isCashPromo = promo.isCashPromo;
			promoGO.subscribeInfo = promo;
			promoGO.promoName.text = promo.GetTitle();
			promoGO.gunPromoTexture[0].mainTexture = iconSubscription;
			promoGO.gunPromoTexture[0] = promoGO.gunPromoTexture[0].ReplaceForCurrentPlatform();
			(promoGO.gunPromoTexture[0] as UITextureMasked).textureAlpha = iconAlphaSubscription;
			promoGO.gunPromoTexture[0].gameObject.SetActive(true);
			promoGO.gunPromoTexture[0].transform.localScale = Vector3.one * 1.2f;
			promoGO.mainPromoTexture.gameObject.SetActive(false);
			promoGO.bonuseMoneyTexture.gameObject.SetActive(false);
			listPromo.Add(promoGO);
			slides.Add(promoGO.gameObject);
			SelectableButton slide = Object.Instantiate(slideButton);
			slide.transform.SetParent(_gridSlide.transform);
			slide.transform.localScale = Vector3.one;
			slide.gameObject.SetActive(true);
			controllerButton.allButtons.Add(slide);
		}
		UpdateMiniStarterPackButtons();
		if (slides.Count == 0)
		{
			mainGO.SetActive(false);
		}
		else
		{
			mainGO.SetActive(true);
		}
		if (slides.Count == 1)
		{
			foreach (SelectableButton element in controllerButton.allButtons)
			{
				element.gameObject.SetActive(false);
			}
		}
		scrollView.ResetPosition();
		yield return null;
		scrollView.ResetPosition();
		_gridPromo.repositionNow = true;
		_gridSlide.repositionNow = true;
		_gridPromo.Reposition();
		_gridSlide.Reposition();
		if (slideCoroutine != null)
		{
			StopCoroutine(slideCoroutine);
		}
		slideCoroutine = StartCoroutine(SlideAnimation());
		indexSlide = 0;
	}

	public void UpdatePromo()
	{
		foreach (PromoItem item in listPromo)
		{
			if (item.subscribeInfo != null)
			{
				item.promoName.text = item.subscribeInfo.GetTitle();
				continue;
			}
			if (!item.info.Enabled)
			{
				item.gameObject.SetActive(false);
				continue;
			}
			item.promoName.text = item.info.GetTitle();
			if (item.info.bonusImageType == InAppPromoController.BonusImageType.Money)
			{
				if (item.isCashPromo)
				{
					item.mainPromoTexture.mainTexture = iconCash;
					item.bonuseMoneyTexture.mainTexture = iconGold;
				}
				else
				{
					item.mainPromoTexture.mainTexture = iconGold;
					item.bonuseMoneyTexture.mainTexture = iconCash;
				}
				item.mainPromoTexture.gameObject.SetActive(true);
				item.bonuseMoneyTexture.gameObject.SetActive(true);
			}
			else
			{
				item.mainPromoTexture.gameObject.SetActive(false);
			}
			if (item.info.promoBonuses.GetPromoMarketBonusesIds().Count == 0)
			{
				continue;
			}
			if (!item.info.promoBonuses.GiveAlternativeBonuses())
			{
				int num = 0;
				UIBasicSprite[] carPromoTexture = item.carPromoTexture;
				foreach (UIBasicSprite uIBasicSprite in carPromoTexture)
				{
					uIBasicSprite.mainTexture = null;
					uIBasicSprite.gameObject.SetActive(false);
				}
				UIBasicSprite[] gunPromoTexture = item.gunPromoTexture;
				foreach (UIBasicSprite uIBasicSprite2 in gunPromoTexture)
				{
					uIBasicSprite2.mainTexture = null;
					uIBasicSprite2.gameObject.SetActive(false);
				}
				foreach (string promoMarketBonusesId in item.info.promoBonuses.GetPromoMarketBonusesIds())
				{
					if (item.info.bonusImageType == InAppPromoController.BonusImageType.Vehicle)
					{
						item.carPromoTexture[num].LoadTextureAndAlphaMask("AtlasShopGuns/", MarketController.Instance.GetProduct(promoMarketBonusesId).texture);
						item.carPromoTexture[num].gameObject.SetActive(true);
						if (MarketController.Instance.GetProduct(promoMarketBonusesId) is ProductTitan)
						{
							item.carPromoTexture[num].width = 128;
							item.carPromoTexture[num].height = 128;
						}
					}
					else if (item.info.bonusImageType == InAppPromoController.BonusImageType.Weapon)
					{
						item.gunPromoTexture[num].LoadTextureAndAlphaMask("AtlasShopGuns/", MarketController.Instance.GetProduct(promoMarketBonusesId).texture);
						item.gunPromoTexture[num].gameObject.SetActive(true);
					}
					num++;
				}
			}
			else
			{
				item.mainPromoTexture.mainTexture = iconGold;
				item.bonuseMoneyTexture.mainTexture = iconCash;
				item.mainPromoTexture.gameObject.SetActive(true);
				item.bonuseMoneyTexture.gameObject.SetActive(true);
				item.SetProductPower(null);
			}
		}
		scrollView.ResetPosition();
		_gridPromo.repositionNow = true;
		_gridSlide.repositionNow = true;
		_gridPromo.Reposition();
		_gridSlide.Reposition();
		if (slideCoroutine != null)
		{
			StopCoroutine(slideCoroutine);
		}
		slideCoroutine = StartCoroutine(SlideAnimation());
		indexSlide = 0;
	}

	private void SelectFromCenter(GameObject centered)
	{
		if (centered == null)
		{
			return;
		}
		int num = 0;
		foreach (GameObject slide in slides)
		{
			if (centered == slide)
			{
				controllerButton.SelectButton(controllerButton.allButtons[num]);
				indexSlide = num;
				break;
			}
			num++;
		}
		MiniStarterPackUI component = centered.GetComponent<MiniStarterPackUI>();
		if (component != null)
		{
			ShowGoldBG(1f);
		}
		else
		{
			ShowGoldBG(0f);
		}
	}

	private void ShowGoldBG(float alphaValue)
	{
		if (bgSpriteTweener != null)
		{
			HOTween.Kill(bgSpriteTweener);
		}
		bgSpriteTweener = HOTween.To(bgSpriteGold, 0.5f, new TweenParms().Prop("alpha", alphaValue).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete((TweenDelegate.TweenCallback)delegate
		{
		}));
	}

	private IEnumerator SlideAnimation()
	{
		while (slides.Count == 0)
		{
			yield return new WaitForSeconds(0.5f);
		}
		centrOnChield.CenterOn(slides[0].transform);
		if (slides.Count == 1)
		{
			yield break;
		}
		while (true)
		{
			yield return new WaitForSeconds(5f);
			if (indexSlide >= slides.Count - 1)
			{
				leftSlide = false;
			}
			else if (indexSlide <= 0)
			{
				leftSlide = true;
			}
			if (leftSlide)
			{
				indexSlide++;
			}
			else
			{
				indexSlide--;
			}
			centrOnChield.CenterOn(slides[Mathf.Clamp(indexSlide, 0, slides.Count - 1)].transform);
		}
	}

	public void UpdateMiniStarterPackButtons()
	{
		ClearMiniStarterPackButtons();
		foreach (StarterPack availableStarterPack in StarterPacksManager.instance.availableStarterPacks)
		{
			if (availableStarterPack.availableForPurcase && availableStarterPack.IsGood())
			{
				AddMiniStarterPackButton(availableStarterPack);
			}
		}
	}

	private void ClearMiniStarterPackButtons()
	{
		foreach (MiniStarterPackUI miniStarterPackButton in miniStarterPackButtons)
		{
			Object.Destroy(miniStarterPackButton.gameObject);
		}
		miniStarterPackButtons.Clear();
	}

	private void AddMiniStarterPackButton(StarterPack starterPack)
	{
		MiniStarterPackUI miniStarterPackUI = Object.Instantiate(this.miniStarterPackUI);
		miniStarterPackUI.transform.SetParent(_gridPromo.transform);
		miniStarterPackUI.transform.localPosition = Vector3.zero;
		miniStarterPackUI.transform.localScale = Vector3.one;
		miniStarterPackUI.Init(starterPack);
		miniStarterPackUI.SetRewards(1f, miniStarterPackUI.dafaultRewardGrid, miniStarterPackUI.starterPack.rewards, true, 4);
		miniStarterPackButtons.Add(miniStarterPackUI);
		miniStarterPackUI.gameObject.SetActive(true);
		slides.Add(miniStarterPackUI.gameObject);
		SelectableButton selectableButton = Object.Instantiate(slideButton);
		selectableButton.transform.SetParent(_gridSlide.transform);
		selectableButton.transform.localScale = Vector3.one;
		selectableButton.gameObject.SetActive(true);
		controllerButton.allButtons.Add(selectableButton);
	}

	public void OnDrag(Vector2 delta)
	{
		StopAutoScroll();
	}

	public void StopAutoScroll()
	{
		if (slideCoroutine != null)
		{
			StopCoroutine(slideCoroutine);
		}
	}
}
